package com.shenmazong.fastdfs.controller;

import com.shenmazong.fastdfs.service.TbUploadImageService;
import com.shenmazong.fastdfs.utils.FastFileUtil;
import com.shenmazong.fastdfs.vo.UploadImageVo;
import com.shenmazong.utils.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2022/2/10 15:24
 */

@RestController
@Slf4j
@CrossOrigin
public class FileUploadController {

    @Autowired
    FastFileUtil fastFileUtil;

    @Autowired
    TbUploadImageService tbUploadImageService;

    @PostMapping(value = "/upload")
    public ResultResponse upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String url = fastFileUtil.uploadFile(multipartFile);
        log.warn(url);

        HashMap<String, Object> map = new HashMap<>();
        map.put("url", url);
        map.put("name", UUID.randomUUID().toString());

        return ResultResponse.SUCCESS(map);
    }

    @PostMapping(value = "/uploadImageFiles")
    public ResultResponse uploadImageFiles(@RequestBody UploadImageVo uploadImageVo) {
        return tbUploadImageService.uploadImageFiles(uploadImageVo.getName(),uploadImageVo.getUrls());
    }

    @PostMapping(value = "/getImage")
    public ResultResponse getImage() {
        return tbUploadImageService.getImage();
    }
}
